package cn.fl.login.utils;

import java.net.InetAddress;

/**
 * UUID工具类
 *
 * @projectName: fl-web
 * @package: cn.fl.login.utils
 * @className: UUIDUtils
 * @author: menghongqiang@lionbridgecapital.cn
 * @createDate: 2020/4/27 11:07
 * @version: V1.0.0
 * @copyright: (c)2020
 * @updateRemark: [序号][YYYY-MM-DD] [更改人姓名][变更描述]
 */
public class UUIDUtils {
    private static UUIDUtils uuid = new UUIDUtils();
    private static final int IP;
    private static short counter;
    private static final int JVM;

    private UUIDUtils() {
    }

    private String generateHex() {
        return (this.format(this.getIP()) + this.format(this.getJVM()) + this.format(this.getHighTime()) + this.format(this.getLowTime()) + this.format(this.getCount())).toUpperCase();
    }

    private String format(int intval) {
        String formatted = Integer.toHexString(intval);
        StringBuilder buf = new StringBuilder("00000000");
        buf.replace(8 - formatted.length(), 8, formatted);
        return buf.toString();
    }

    private String format(short shortval) {
        String formatted = Integer.toHexString(shortval);
        StringBuilder buf = new StringBuilder("0000");
        buf.replace(4 - formatted.length(), 4, formatted);
        return buf.toString();
    }

    private int getJVM() {
        return JVM;
    }

    private short getCount() {
        Class var1 = UUIDUtils.class;
        synchronized(UUIDUtils.class) {
            if (counter < 0) {
                counter = 0;
            }

            return counter++;
        }
    }

    private int getIP() {
        return IP;
    }

    private short getHighTime() {
        return (short)((int)(System.currentTimeMillis() >>> 32));
    }

    private int getLowTime() {
        return (int)System.currentTimeMillis();
    }

    private static int toInt(byte[] bytes) {
        int result = 0;

        for(int i = 0; i < 4; ++i) {
            result = (result << 8) - -128 + bytes[i];
        }

        return result;
    }

    public static String uuid() {
        return uuid.generateHex();
    }

    static {
        int ipadd;
        try {
            ipadd = toInt(InetAddress.getLocalHost().getAddress());
        } catch (Exception var2) {
            ipadd = 0;
        }

        IP = ipadd;
        counter = 0;
        JVM = (int)(System.currentTimeMillis() >>> 8);
    }
}
